package cn.pomit.skill.groovy.rule;

import java.util.ArrayList;
import java.util.List;

import cn.pomit.skill.groovy.rule.db.Rules;
import cn.pomit.skill.groovy.rule.db.RulesDao;
import cn.pomit.skill.groovy.rule.normal.NormalJavaRule;

public class RuleFactory {
	private static volatile RuleFactory singleton = null;

	private RuleFactory() {
	}
	public static RuleFactory getInstance() {
		// 第一次校验singleton是否为空
		if (singleton == null) {
			synchronized (RuleFactory.class) {
				// 第二次校验singleton是否为空
				if (singleton == null) {
					singleton = new RuleFactory();
				}
			}
		}
		return singleton;
	}

	private RulesDao rulesDao;
	private RuleFactory(RulesDao rulesDao) {
		this.rulesDao = rulesDao;
	}
	public static RuleFactory getInstance(RulesDao rulesDao) {
		// 第一次校验singleton是否为空
		if (singleton == null) {
			synchronized (RuleFactory.class) {
				// 第二次校验singleton是否为空
				if (singleton == null) {
					singleton = new RuleFactory(rulesDao);
				}
			}
		}
		return singleton;
	}

	public List<IRule> getRuleList() throws Exception {
		//调用普通的JAVA实现做对比
		List<IRule> rules = new ArrayList<>();
		NormalJavaRule normalJavaRule = new NormalJavaRule();
		rules.add(normalJavaRule);

		//直接读取Groovy文件生成IRule实现
		IRule groovyFile = GroovyFactory.getInstance()
				.getIRuleFromPackage("cn/pomit/skill/groovy/rule/file/GroovyFileRule.groovy");
		rules.add(groovyFile);

		//从Db的Groovy脚本中生成IRule实现
		Rules ruleGroovy = rulesDao.getByName("GroovyDbRule");
		if (ruleGroovy != null) {
			IRule groovyDb = GroovyFactory.getInstance().getIRuleFromCode(ruleGroovy.getRule());
			rules.add(groovyDb);
		}

		return rules;
	}
}
